//
// Created by wangwanlong on 2024/6/19.
//

#ifndef MCGPP_MATH_H
#define MCGPP_MATH_H
namespace mcgpp {
  namespace math {
    template<typename T>
    static inline T cs2(const T &x) {
      return x * x;
    }

    template<typename T>
    static inline T hash(const T &x) {
      return cs2(x);
    }
  }

  //比较浮点数是否近似相等
  template<class T>
  typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
  almost_equal(T x, T y, int ulp) {
    // the machine epsilon has to be scaled to the magnitude of the values used
    // and multiplied by the desired precision in ULPs (units in the last place)
    return std::abs(x - y) <= std::numeric_limits<T>::epsilon() * std::abs(x + y) * ulp
           // unless the result is subnormal
           || std::abs(x - y) < std::numeric_limits<T>::min();
  }

}
#endif //MCGPP_MATH_H
